package frog.lazy.framework.keeper.reaml;


import cn.hutool.crypto.SecureUtil;
import frog.lazy.framework.keeper.dto.JwtTokenDTO;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
 *   这里是登录认证realm
 */
public class PasswordRealm extends AuthorizingRealm {

    @Override
    public Class<?> getAuthenticationTokenClass() {
        return JwtTokenDTO.class;
    }

    /**
     * 授权
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        return info;
    }

    /**
     * 登录认证
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        JwtTokenDTO accountDTO = (JwtTokenDTO)authenticationToken;
        accountDTO.setPassword(SecureUtil.md5(accountDTO.getPassword()));
        return new SimpleAuthenticationInfo(accountDTO, accountDTO.getPassword(), getName());
    }
}
